<?php

namespace app\admin\service;

use app\model\OfficialAccounts;
use app\model\ResponsibilityPerson;
use app\model\Company;
use app\model\Applet;

class OfficialAccountsService
{
    private $officialAccountsModel;
    private $responsibilityPersonModel;
    private $companyModel;
    private $appletModel;

    public function __construct(OfficialAccounts $officialAccountsModel, ResponsibilityPerson $responsibilityPersonModel, Company $companyModel, Applet $appletModel)
    {
        $this->officialAccountsModel = $officialAccountsModel;
        $this->responsibilityPersonModel = $responsibilityPersonModel;
        $this->companyModel = $companyModel;
        $this->appletModel = $appletModel;
    }

    // 添加/编辑公众号
    public function edit($params)
    {
        if (empty($params['id'])) {
            $params['created_at'] = time();
        } else {
            $params['updated_at'] = time();
        }

        // 判断该小程序是否有默认关联公众号
        $data = $this->officialAccountsModel->getInfoByWhere(['applet_id' => $params['applet_id'], 'is_default' => 1], 'id');
        if ($params['is_default'] != 1) {
            if (!$data) {
                $params['is_default'] = 1;
            }
        } else {
            if ($data) {
                $this->officialAccountsModel->edit(['id' => $data['id'], 'is_default' => 0]);
            }
        }

        $id = $this->officialAccountsModel->edit($params);

        if (!$id) {
            return false;
        }

        $appletData = $this->appletModel->getInfoById($params['applet_id'], 'app_id');
        $content = '<a data-miniprogram-appid="' . $appletData['app_id'] . '" data-miniprogram-path="pages/Small-theater/Small-theater?official_account_id=' . $id . '&uid=">点击观看短剧</a>';
        $bool = $this->officialAccountsModel->edit(['id' => $id, 'content' => $content]);

        if (!$bool) {
            return false;
        }

        return true;
    }

    // 获取公众号列表
    public function getList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        if (!empty($params['id'])) {
            $where[] = ['id', '=', $params['id']];
        }

        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%'.$params['name'].'%'];
        }

        $data = $this->officialAccountsModel->getPagingDataByWhere($where, $page, $limit, 'id, name, original_id, company_id, applet_id');
        if ($data['total'] == 0) {
            return $data;
        }

        // 公司
        $companyIds = array_column($data['data'], 'company_id');
        $companyData = $this->companyModel->getDataByWhere(['id' => $companyIds], 'id, name');
        $companyResult = array_column($companyData, 'name', 'id');
        // 负责人
        $officialAccountIds = array_column($data['data'], 'id');
        $responsibilityPersonData = $this->responsibilityPersonModel->getDataByWhere([], 'id, official_account_id');
        // 微信小程序
        $appletIds = array_column($data['data'], 'applet_id');
        $appletData = $this->appletModel->getDataByWhere(['id' => $appletIds], 'id, name');
        $applet = array_column($appletData, 'name', 'id');

        foreach ($data['data'] as $key => $item) {
            $number = 0;
            foreach ($responsibilityPersonData as $value) {
                $officialAccountIdArray = explode(',', $value['official_account_id']);
                if (in_array($item['id'], $officialAccountIdArray)) {
                    $number = $number + 1;
                }
            }
            $data['data'][$key]['company_name'] = empty($companyResult[$item['company_id']]) ? '' : $companyResult[$item['company_id']];
            $data['data'][$key]['number'] = $number;

            $data['data'][$key]['applet_name'] = empty($applet[$item['applet_id']]) ? '' : $applet[$item['applet_id']];
        }
        return $data;
    }

    // 获取公众号详情
    public function getOfficialAccountsDetail($id)
    {
        $data = $this->officialAccountsModel->getInfoById($id, 'id, company_id, name, original_id, app_id, app_secret, token, encoding_aes_key, applet_id, content, is_default');
        return $data;
    }

    // 删除指定公众号
    public function deleteOfficialAccount($id)
    {
        $bool = $this->officialAccountsModel->destroy($id);
        if (!$bool) {
            return false;
        }
        return true;
    }

    // 获取指定公司的下拉列表
    public function getOfficialAccountsSelectList($companyId)
    {
        $data = $this->officialAccountsModel->getDataByWhere(['company_id' => $companyId]);
        return $data;
    }
}